
class_name CPlayerMoneyArray
extends EventDispatcher

signal UUID_changed(old_value, new_value) 
signal MaskID_changed(old_value, new_value) 
signal Type_changed(old_value, new_value) 
signal CurMoney_changed(old_value, new_value) 
signal TotalMoney_changed(old_value, new_value) 



class SPlayerMoney:
    var nIndex
    var tblOwner
    var m_tblAlias
    var bInited = false
    func _init(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.Type = 0	# 货币类型
        self.CurMoney = 0	# 当前货币数量
        self.TotalMoney = 0	# 货币总数

        self.nIndex = -1
        self.tblOwner = owner
        self.m_tblAlias = {
 
        }

    func GetIndex():
        return self.nIndex

    func SetZeroValue(owner):
        self.UUID = 0
        self.MaskID = owner and owner.m_UUID or 0
        self.Type = 0	# 货币类型
        self.CurMoney = 0	# 当前货币数量
        self.TotalMoney = 0	# 货币总数


    func UnPack(_bufferStream, _bOffset):
        if _bOffset :
            _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)

        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.Type = _bufferStream.ReadInt32()
        self.CurMoney = _bufferStream.ReadUint32()
        self.TotalMoney = _bufferStream.ReadUint32()

        self.bInited = true
        if self.tblOwner :
            self.tblOwner.DispatchEvent('uuid', [self, 0, self.nIndex])

    func CopyFrom(other):
        self.nIndex = other.nIndex
        self.Type = other.Type
        self.CurMoney = other.CurMoney
        self.TotalMoney = other.TotalMoney


    func Update(_bufferStream, _nIndex ):
        _bufferStream.SetPosition(_bufferStream.GetPosition() + 4)
        self.UUID = _bufferStream.ReadInt64()
        self.MaskID = _bufferStream.ReadInt64()
        self.Type = _bufferStream.ReadInt32()
        self.CurMoney = _bufferStream.ReadUint32()
        self.TotalMoney = _bufferStream.ReadUint32()


    func OnDataChanged( fieldName, oldValue, newValue ):
        if oldValue == newValue or not self.tblOwner:
            return
        self.tblOwner.emit_signal(fieldName, oldValue, newValue)


    #  SPlayerMoney Geter Setter method # 
    var UUID:
        set(_value):
            var old = self.m_UUID
            self.UUID = _value
            if self.tblOwner:
                self.tblOwner.UUID_changed(old, _value)
        get:
            return UUID

    var MaskID:
        set(_value):
            var old = self.m_MaskID
            self.MaskID = _value
            if self.tblOwner:
                self.tblOwner.MaskID_changed(old, _value)
        get:
            return MaskID

    var Type:
        set(_value):
            var old = self.m_Type
            self.Type = _value
            if self.tblOwner:
                self.tblOwner.Type_changed(old, _value)
        get:
            return Type

    var CurMoney:
        set(_value):
            var old = self.m_CurMoney
            self.CurMoney = _value
            if self.tblOwner:
                self.tblOwner.CurMoney_changed(old, _value)
        get:
            return CurMoney

    var TotalMoney:
        set(_value):
            var old = self.m_TotalMoney
            self.TotalMoney = _value
            if self.tblOwner:
                self.tblOwner.TotalMoney_changed(old, _value)
        get:
            return TotalMoney



# ================================= array ==========================
var m_UUID
var m_nPlayerMoneyCount
var m_PlayerMoney

signal elem_update(array, old_value, new_value, index)
signal elem_add(array, old_value, new_value, index)
signal elem_del(array, old_value, new_value, index)

func _init( _uuid ):
    self.m_UUID = _uuid
    self.m_nPlayerMoneyCount = 0
    self.m_PlayerMoney = []
    var nMaxIndex = self.GetPlayerMoneyMaxCount()-1
    self.m_PlayerMoney.resize(nMaxIndex)
    for i in range(nMaxIndex):
        self.m_PlayerMoney[i] = SPlayerMoney.new(self) 
        self.m_PlayerMoney[i].nIndex = i

func UnPack(_bufferStream):
    self.m_UUID = _bufferStream.ReadInt64()
    self.m_nPlayerMoneyCount = _bufferStream.ReadUint32()
    for i in range(self.m_nPlayerMoneyCount):
        var elem = SPlayerMoney.new(self)
        elem.nIndex = i
        elem.UnPack(_bufferStream, false)
        self.m_PlayerMoney[i] = elem

func GetTableName():
    return 'PlayerMoney'

func GetUUID():
    return self.m_UUID

func GetPlayerMoneyCurNum():
    return self.m_nPlayerMoneyCount

func SetPlayerMoneyCurNum( nValue ):
    self.m_nPlayerMoneyCount = nValue

func SetCurNum(nValue):
    self.m_nPlayerMoneyCount = nValue

static func GetPlayerMoneyMaxCount():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY

func GetPlayerMoneyMaxNum():
    return TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY

func GetPlayerMoney(_nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY:
        print("GetPlayerMoneyEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY")
        return null

    return self.m_PlayerMoney[_nIndex];

func SetPlayerMoney(_Prop, _nIndex):
    if _nIndex < 0 or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY:
        print("SetPlayerMoneyEle _nIndex > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY")
        return null
    
    var old = self.m_PlayerMoney[_nIndex]
    var elem = SPlayerMoney.new(self)
    elem.CopyFrom(_Prop)
    elem.nIndex = _nIndex
    self.m_PlayerMoney[_nIndex] = elem
    elem_update.emit(self, old, elem, _nIndex)
    return elem


func AddPlayerMoney(_Prop, _nIndex):
    var nCurNum = self.m_nPlayerMoneyCount
    if _nIndex < 0 or _nIndex > nCurNum:
        _nIndex = nCurNum

    if not _Prop or _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY:
        print("AddPlayerMoneyEle _nIndex >= TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY");
        return null;

    var elem = SPlayerMoney.new(self)
    elem.CopyFrom(_Prop)
    if _nIndex < nCurNum:
        for i in range(nCurNum-1, -1, -1):
            self.m_PlayerMoney[i] = self.m_PlayerMoney[i - 1]
            self.m_PlayerMoney[i].nIndex = i
    self.m_PlayerMoney[_nIndex] = elem
    self.m_nPlayerMoneyCount = nCurNum + 1
    elem_add.emit(self, null, elem, _nIndex)

func DelPlayerMoney(_nIndex):
    if _nIndex < 0 or _nIndex >= self.m_nPlayerMoneyCount:
        return
    
    self.m_nPlayerMoneyCount = self.m_nPlayerMoneyCount - 1
    if _nIndex != self.m_nPlayerMoneyCount:
        self.m_PlayerMoney[_nIndex] = self.m_PlayerMoney[self.m_nPlayerMoneyCount]

    # var nLastDataID = self.m_PlayerMoney[self.m_nPlayerMoneyCount]
    var elem = self.m_PlayerMoney[_nIndex]
    for i in range(_nIndex + 1, self.m_nPlayerMoneyCount - 1):
        self.m_PlayerMoney[i - 1] = self.m_PlayerMoney[i]
        self.m_PlayerMoney[i - 1].nIndex = i - 1
    self.m_PlayerMoney[self.m_nPlayerMoneyCount - 1] = null
    elem_del.emit(self, elem, null, _nIndex)


func CopyFrom(_pOther):
    self.m_nPlayerMoneyCount = _pOther.m_nPlayerMoneyCount
    for i in range(_pOther.m_nPlayerMoneyCount):
        self.m_PlayerMoney[i] = SPlayerMoney.new(self)
        self.m_PlayerMoney[i].CopyFrom(_pOther.m_PlayerMoney[i])

func UnPackField(_fieldId, _bufferStream, _varLen):
    var elemCount = self.m_nPlayerMoneyCount
    if _fieldId < 0 or _fieldId > TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY:
        # var szLog = string.format("CPlayerMoneyArray.UnPackField Error :\
        #  _fieldId(%d) < 0 or _fieldId(%d) > self.MaxPlayerMoneyEleCount()(%d)",
        #  _fieldId, _fieldId, TableCommonDefine.MaxRowElem.MAX_COUNT_PLAYERMONEY)
        return false
    var elem = null
    var old = null
    if self.m_PlayerMoney[_fieldId] != null:
        elem = self.m_PlayerMoney[_fieldId]
        old = SPlayerMoney.new( self )
        old.CopyFrom( elem )
    else:
        elem = SPlayerMoney.new(self)
        self.m_PlayerMoney[_fieldId] = elem
        self.m_PlayerMoney[_fieldId].nIndex = _fieldId

    if elemCount <= _fieldId:
        self.m_nPlayerMoneyCount = _fieldId + 1

    elem.Update(_bufferStream, _fieldId )
    elem_update.emit(self, old, elem, _nIndex)
    return true

func AddField(_fieldId, _bufferStream):
    var elem = SPlayerMoney.new(self)
    elem.UnPack(_bufferStream, true )
    elem.nIndex = _fieldId
    if null == self.m_PlayerMoney[_fieldId]:
        self.m_PlayerMoney[_fieldId] = elem
    else:
        var uuid = elem.m_UUID;
        for index in range(self.m_nPlayerMoneyCount, _fieldId+1, -1):
            self.m_PlayerMoney[index] = self.m_PlayerMoney[index- 1]
            self.m_PlayerMoney[index].m_UUID = uuid + index - _fieldId
            self.m_PlayerMoney[index].nIndex = index
        self.m_PlayerMoney[_fieldId] = elem;
        self.m_PlayerMoney[_fieldId].nIndex = _fieldId

    self.m_nPlayerMoneyCount = self.m_nPlayerMoneyCount + 1;
    elem_add.emit(self, null, elem, _fieldId)

func DelField(_fieldId ):
    if self.m_PlayerMoney[_fieldId] == null:
        return 
    
    self.m_nPlayerMoneyCount = self.m_nPlayerMoneyCount - 1
    var elem = self.m_PlayerMoney[_fieldId]
    if _fieldId != self.m_nPlayerMoneyCount:
        var uuid = self.m_PlayerMoney[_fieldId].m_UUID;
        self.m_PlayerMoney[_fieldId] = self.m_PlayerMoney[self.m_nPlayerMoneyCount]
        self.m_PlayerMoney[_fieldId].m_UUID = uuid
        self.m_PlayerMoney[_fieldId].nIndex = _fieldId

    self.m_PlayerMoney[self.m_nPlayerMoneyCount] = null
    elem_del.emit(self, elem, null, _fieldId)   

func Filter( funFilter ):
    var tblItems = []
    for i in range(self.m_nPlayerMoneyCount-1):
        if funFilter.call(self.m_PlayerMoney[i], i):
            tblItems.append(self.m_PlayerMoney[i])
    return tblItems

func QueryByType(Type):
    var nCnt = self.m_nPlayerMoneyCount
    for i in range(nCnt-1):
        var curElem = self.m_PlayerMoney[i]
        if curElem != null :
            if curElem.m_MaskID > 0 and curElem.m_Type == Type :
                return curElem
    return null


